package com.jframe.sys.controller.utils;

import java.lang.reflect.Field;
import java.util.List;

import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.StringUtil;

public class QueryHelp {

	private String conditions;
	private Class clazz;
	
	public QueryHelp(){
		
	}
	
	public QueryHelp(String conditions,Class clazz){
		this.conditions=conditions;
		this.clazz = clazz;
	}

	public String getQueryStr(){
		String query = "";
		if(conditions.equals("")){
			return "";
		}
		//进行查询字段过滤，避免出现非法ｓｑｌ注入
		Field[] fields = clazz.getDeclaredFields();
		List<Condition> cdtsls = JsonUtil.json2List(conditions, Condition.class);
		String fieldstr = "";
		for(Field field:fields){
			fieldstr +=field.getName()+",";
		}
		for(Condition condition:cdtsls){
			if(condition.getLogic().equals("and")){
				if(condition.getOperators().equals("=")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+"=?";
					}
				}else if(condition.getOperators().equals("like")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+" like ?";
					}
				}else if(condition.getOperators().equals(">")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+">?";
					}
				}else if(condition.getOperators().equals(">=")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+">=?";
					}
				}else if(condition.getOperators().equals("<")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+"<?";
					}
				}else if(condition.getOperators().equals("<=")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+"<=?";
					}
				}else if(condition.getOperators().equals("in")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " and "+condition.getName()+" in ?";
					}
				}
			}else if(condition.getLogic().equals("or")){
				if(condition.getOperators().equals("=")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " or "+condition.getName()+"=?";
					}	
				}else if(condition.getOperators().equals("like")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " or "+condition.getName()+" like ?";
					}
				}else if(condition.getOperators().equals(">")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " or "+condition.getName()+">?";
					}
				}else if(condition.getOperators().equals(">=")){
					if(fieldstr.indexOf(condition.getName())!=-1){
						query += " or "+condition.getName()+">=?";
					}
				}else if(condition.getOperators().equals("<")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " or "+condition.getName()+"<?";
					}
				}else if(condition.getOperators().equals("<=")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " or "+condition.getName()+"<=?";
					}
				}else if(condition.getOperators().equals("in")){
					if(fieldstr.indexOf(condition.getName())>=1){
						query += " or "+condition.getName()+" in ?";
					}
				}
			}
		}
		return query;
	}
	
	public Object[] getParams(){
		if(conditions.equals("")){
			return null;
		}
		List<Condition> cdtsls = JsonUtil.json2List(conditions, Condition.class);
		Object [] objs = new Object[cdtsls.size()];
		for(int i=0;i<cdtsls.size();i++){
			Condition condition = cdtsls.get(i);
			if(condition.getOperators().equals("like")){
				objs[i] = "%"+cdtsls.get(i).getValue()+"%";
			}else if(condition.getOperators().equals("in")){
				objs[i] = "("+cdtsls.get(i).getValue()+")";
			}else{
				if(condition.getType().equals("int")||condition.getType().equals("Integer")){
					objs[i]= StringUtil.parseInt(cdtsls.get(i).getValue());
				}else{
					objs[i]=cdtsls.get(i).getValue();
				}
			}
		}
		return objs;
	}
	
	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}
	
}
